﻿using System;
using System.Globalization;
using Common;
using DocumentFormat.OpenXml.Wordprocessing;

namespace OpenXmlEasy.Word
{
    public partial class WordEasy
    {
        #region Title

        private int _titleNumL1 = 1;
        private int _titleNumL2 = 1;
        private int _titleNumL3 = 1;

        public void InsertTitle(string title, bool isBegin = false)
        {
            InsertTitle(title, 1, isBegin);
        }

        public void InsertTitle(string title, int level, bool isBegin = false)
        {
            if (isBegin)
            {
                if (level == 1)
                {
                    _titleNumL1 = 1;
                }
                if (level <= 2)
                {
                    _titleNumL2 = 1;
                }
                if (level <= 3)
                {
                    _titleNumL3 = 1;
                }
                else
                {
                    throw new Exception("标题级别设置错误");
                }
            }
            var titlePre = string.Empty;
            switch (level)
            {
                case 1:
                    titlePre = string.Format("{0}、", NumberParse.Convertdigit(_titleNumL1.ToString(CultureInfo.InvariantCulture)));
                    _titleNumL1++;
                    break;
                case 2:
                    titlePre = string.Format("（{0}）", NumberParse.Convertdigit(_titleNumL2.ToString(CultureInfo.InvariantCulture)));
                    _titleNumL2++;
                    break;
                case 3:
                    titlePre = string.Format("{0}.", _titleNumL3.ToString(CultureInfo.InvariantCulture));
                    _titleNumL3++;
                    break;
            }
            var fullTitle = string.Format("{0}{1}", titlePre, title);
            if (level == 1)
            {
                _body.AppendChild(GenerateParagraphOfTitle1(fullTitle));
                Console.WriteLine("插入：" + fullTitle);
            }
            else if (level == 2)
            {
                _body.AppendChild(GenerateParagraphOfTitle2(fullTitle));
                Console.WriteLine("插入：" + fullTitle);
            }
            else if (level == 3)
            {
                _body.AppendChild(GenerateParagraphOfTitle3(fullTitle));
                Console.WriteLine("插入：    " + fullTitle);
            }
            else
            {
                throw new ApplicationException("不支持3级以下标题");
            }
        }

        #endregion

        public void InsertTitle(string title, string numPrefix, bool isBegin = false, params string[] titleArgs)
        {
            throw new NotImplementedException();
        }

        public Paragraph GenerateParagraphOfTitle1(string title)
        {
            var paragraph1 = new Paragraph { RsidParagraphAddition = "00C8115C", RsidParagraphProperties = "00A204AD", RsidRunAdditionDefault = "005C74D0" };

            var paragraphProperties1 = new ParagraphProperties();
            var paragraphStyleId1 = new ParagraphStyleId { Val = "heading 1" };

            var paragraphMarkRunProperties1 = new ParagraphMarkRunProperties();
            var runFonts1 = new RunFonts { Hint = FontTypeHintValues.EastAsia };

            paragraphMarkRunProperties1.AppendChild(runFonts1);

            paragraphProperties1.AppendChild(paragraphStyleId1);
            paragraphProperties1.AppendChild(paragraphMarkRunProperties1);

            var run1 = new Run();

            var runProperties1 = new RunProperties();
            var runFonts2 = new RunFonts { Hint = FontTypeHintValues.EastAsia };

            runProperties1.AppendChild(runFonts2);
            var lastRenderedPageBreak1 = new LastRenderedPageBreak();
            var text1 = new Text { Text = title };

            run1.AppendChild(runProperties1);
            run1.AppendChild(lastRenderedPageBreak1);
            run1.AppendChild(text1);

            paragraph1.AppendChild(paragraphProperties1);
            paragraph1.AppendChild(run1);
            return paragraph1;
        }

        public Paragraph GenerateParagraphOfTitle2(string title)
        {
            var paragraph1 = new Paragraph { RsidParagraphAddition = "00C8115C", RsidParagraphProperties = "00A204AD", RsidRunAdditionDefault = "005C74D0" };

            var paragraphProperties1 = new ParagraphProperties();
            var paragraphStyleId1 = new ParagraphStyleId { Val = "heading 2" };

            var paragraphMarkRunProperties1 = new ParagraphMarkRunProperties();
            var runFonts1 = new RunFonts { Hint = FontTypeHintValues.EastAsia };

            paragraphMarkRunProperties1.AppendChild(runFonts1);

            paragraphProperties1.AppendChild(paragraphStyleId1);
            paragraphProperties1.AppendChild(paragraphMarkRunProperties1);

            var run1 = new Run();

            var runProperties1 = new RunProperties();
            var runFonts2 = new RunFonts { Hint = FontTypeHintValues.EastAsia };

            runProperties1.AppendChild(runFonts2);
            var lastRenderedPageBreak1 = new LastRenderedPageBreak();
            var text1 = new Text { Text = title };

            run1.AppendChild(runProperties1);
            run1.AppendChild(lastRenderedPageBreak1);
            run1.AppendChild(text1);

            paragraph1.AppendChild(paragraphProperties1);
            paragraph1.AppendChild(run1);
            return paragraph1;
        }

        public Paragraph GenerateParagraphOfTitle3(string title)
        {
            var paragraph1 = new Paragraph { RsidParagraphAddition = "0015032F", RsidParagraphProperties = "00A204AD", RsidRunAdditionDefault = "0015032F" };

            var paragraphProperties1 = new ParagraphProperties();
            var paragraphStyleId1 = new ParagraphStyleId { Val = "heading 3" };

            paragraphProperties1.AppendChild(paragraphStyleId1);

            var run1 = new Run();

            var runProperties1 = new RunProperties();
            var runFonts1 = new RunFonts { Hint = FontTypeHintValues.EastAsia };

            runProperties1.AppendChild(runFonts1);
            var text1 = new Text { Text = title };

            run1.AppendChild(runProperties1);
            run1.AppendChild(text1);
            var bookmarkStart1 = new BookmarkStart { Name = "_GoBack", Id = "0" };
            var bookmarkEnd1 = new BookmarkEnd { Id = "0" };

            paragraph1.AppendChild(paragraphProperties1);
            paragraph1.AppendChild(run1);
            paragraph1.AppendChild(bookmarkStart1);
            paragraph1.AppendChild(bookmarkEnd1);
            return paragraph1;
        }

    }
}
